package ddnsclient;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author dvtrung
 */
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 *
 * @author MINHHOANG
 */
public class LoginGUI implements ActionListener{

    static JFrame frameLogin;
    private JLabel labelUsername = new JLabel("Username : ");
    private JLabel labelPassword = new JLabel("Password : ");
    private JLabel labelDDNSServer = new JLabel("DDNSServer : ");
    private JLabel labelHostname = new JLabel();


    private static JTextField textUsername = new JTextField();
    private static JPasswordField textPassword = new JPasswordField();
    private static JTextField textDDNSServer = new JTextField("127.0.0.1");
    private static JTextField textTimeUpdate = new JTextField("180");

     public String getHostname()
     {
        return labelHostname.getText().trim();
    }
    public static String getTextTimeUpdate() 
    {
        if(textTimeUpdate.getText() == null || textTimeUpdate.getText().equals(""))
         {
             return "180";
         }
        return textTimeUpdate.getText();
    }
    private JButton buttonConnect = new JButton("Connect");

    public String getUsername() {
        return textUsername.getText().toString();
    }

    public String getPassword() {
        return textPassword.getText().toString();
    }

    public String getDDNSServer() {
        return textDDNSServer.getText().toString();
    }

    public boolean Checkingblank() {
        if (textUsername.getText().toString().equals("")) {
            JOptionPane.showMessageDialog(new JFrame(), "Please fill the Username!", "Error", JOptionPane.OK_OPTION);
            return false;
        } else if (textPassword.getText().toString().equals("")) {
            JOptionPane.showMessageDialog(new JFrame(), "Please fill the Password!", "Error", JOptionPane.OK_OPTION);
            return false;
        } else if (textDDNSServer.getText().equals("")) {
            JOptionPane.showMessageDialog(new JFrame(), "Please fill the DDNSServer!", "Error", JOptionPane.OK_OPTION);
            return false;
        }
        return true;
    }

    public void AddComponentToPane(Container paneLogin) {
        paneLogin.setLayout(new GridBagLayout());
        GridBagConstraints constraint = new GridBagConstraints();

        constraint.fill = GridBagConstraints.HORIZONTAL;
        constraint.weightx = 0.5;

        constraint.insets = new Insets(2, 0, 3, 5);
        constraint.gridx = 0;
        constraint.gridy = 0;
        paneLogin.add(new JLabel("Hostname: "), constraint);

        constraint.gridx = 0;
        constraint.gridy = 1;
        paneLogin.add(labelUsername, constraint);

        constraint.gridx = 0;
        constraint.gridy = 2;
        paneLogin.add(labelPassword, constraint);

        constraint.gridx = 0;
        constraint.gridy = 3;
        paneLogin.add(labelDDNSServer, constraint);

        constraint.gridx = 0;
        constraint.gridy = 4;
        paneLogin.add(new JLabel("Time Update(secs):"), constraint);

        //constraint.gridwidth = 10;
        Main.host = JProcess.getHostName();
        labelHostname.setText(Main.host);
        constraint.ipadx = 250;
        constraint.gridx = 1;
        constraint.gridy = 0;
        paneLogin.add(labelHostname, constraint);

        constraint.gridx = 1;
        constraint.gridy = 1;
        paneLogin.add(textUsername, constraint);

        constraint.gridx = 1;
        constraint.gridy = 2;
        paneLogin.add(textPassword, constraint);

        constraint.gridx = 1;
        constraint.gridy = 3;
        paneLogin.add(textDDNSServer, constraint);
        constraint.ipadx = 0;

        constraint.gridx = 1;
        constraint.gridy = 4;
        paneLogin.add(textTimeUpdate, constraint);
        constraint.ipadx = 0;

        constraint.gridx = 2;
        constraint.gridy = 2;
        paneLogin.add(buttonConnect, constraint);
        buttonConnect.setActionCommand("btnConnect");
        buttonConnect.addActionListener(this);
        
    }

    public void CreateAndShowGUI() {
        frameLogin = new JFrame("Login");
        frameLogin.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        //code here
        this.AddComponentToPane(frameLogin.getContentPane());
        frameLogin.setLocation(300, 200);
        frameLogin.pack();
        frameLogin.setVisible(true);
    }

    public void CloseFrame() {
        frameLogin.setVisible(false);
        frameLogin.dispose();
    }

    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getActionCommand().equals("btnConnect"))
        {
            MyConnection connection = new MyConnection();

            boolean blank = Checkingblank();//kiểm tra các textfield trong khung nhập login
            if (blank == true)
            {
                boolean done = connection.ConnectServer();//thiết lập kết nối đến server
                if (done == false)
                {//kết nối thất bại
                    JOptionPane.showMessageDialog(new JFrame(), "Connection fail", "Error", JOptionPane.OK_OPTION, null);
                } else
                {//thành công
                    JOptionPane.showMessageDialog(new JFrame(), "Connection Success", "Connected", JOptionPane.INFORMATION_MESSAGE, null);
                    CloseFrame();
                    RunningGUI running = new RunningGUI();
                    running.setHostname(connection.getHostname());
                    running.CreateAndShowGUI();
                }
            }
        }
    }
}

